﻿using CurveFitting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace LineChartControlTest
{
    /// <summary>
    /// LinearRegressionFitting.xaml 的交互逻辑
    /// </summary>
    public partial class LinearRegressionFitting : Window
    {
        public LinearRegressionFitting()
        {
            InitializeComponent();
            AddData();
        }

        private void AddData()
        {
            double[] x = new double[] { 0, 1, 2, 3, 4, 5 };
            double[] y = new double[] { 2, 1, 4, 4, 3, 2 };

            LineCharts.FittingCurve ds = new LineCharts.FittingCurve();
            ds.LineColor = Brushes.Blue;
            ds.XDrawStart = -1;
            ds.XDrawEnd = 6;
            ds.RateXValue = 3;
            ds.RegressionFunc = new CurveFittingAlgorithms.ModelFunction[] { f0, f1 };
            ds.FittingMode = LineCharts.FittingCurve.FittingModeEnum.LinearRegression;
            ds.LineThickness = 2;
            ds.SeriesName = "Straight Fit-m2 and Original";
            ds.Symbols.SymbolType = LineCharts.Symbols.SymbolTypeEnum.Diamond;
            ds.Symbols.SymbolSize = 15;
            ds.Symbols.FillColor = Brushes.Black;
            ds.Symbols.BorderColor = Brushes.Black;
            for (int i = 0; i < x.Length; i++)
            {
                ds.RawData.Add(x[i], y[i]);
            }
            myLineChart.SeriesCollection.AddRawData(ds);

            LineCharts.FittingCurve ds1 = new LineCharts.FittingCurve();
            ds1.LineColor = Brushes.Red;
            ds1.XDrawStart = -1;
            ds1.XDrawEnd = 6;
            ds1.RateXValue = 3;
            ds1.RegressionFunc = new CurveFittingAlgorithms.ModelFunction[] { f0, f1, f2 };
            ds1.LinePattern = LineCharts.LinePatternEnum.Dot;
            ds1.FittingMode = LineCharts.FittingCurve.FittingModeEnum.LinearRegression;
            ds1.LineThickness = 2;
            ds1.SeriesName = "Straight Fit-m3";
            ds1.Symbols.SymbolType = LineCharts.Symbols.SymbolTypeEnum.None;
            ds1.Symbols.FillColor = Brushes.Transparent;
            ds1.Symbols.BorderColor = Brushes.Transparent;
            for (int i = 0; i < x.Length; i++)
            {
                ds1.RawData.Add(x[i], y[i]);
            }
            myLineChart.SeriesCollection.AddRawData(ds1);

            LineCharts.FittingCurve ds2 = new LineCharts.FittingCurve();
            ds2.LineColor = Brushes.Green;
            ds2.XDrawStart = -1;
            ds2.XDrawEnd = 6;
            ds2.RateXValue = 3;
            ds2.LinePattern = LineCharts.LinePatternEnum.Dash;
            ds2.RegressionFunc = new CurveFittingAlgorithms.ModelFunction[] { f0, f1, f2, f3 };
            ds2.FittingMode = LineCharts.FittingCurve.FittingModeEnum.LinearRegression;
            ds2.LineThickness = 2;
            ds2.SeriesName = "Straight Fit-m4";
            ds2.Symbols.SymbolType = LineCharts.Symbols.SymbolTypeEnum.None;
            ds2.Symbols.FillColor = Brushes.Transparent;
            ds2.Symbols.BorderColor = Brushes.Transparent;
            for (int i = 0; i < x.Length; i++)
            {
                ds2.RawData.Add(x[i], y[i]);
            }
            myLineChart.SeriesCollection.AddRawData(ds2);

            LineCharts.FittingCurve ds3 = new LineCharts.FittingCurve();
            ds3.LineColor = Brushes.Fuchsia;
            ds3.XDrawStart = -1;
            ds3.XDrawEnd = 6;
            ds3.RateXValue = 3;
            ds3.LinePattern = LineCharts.LinePatternEnum.DashDot;
            ds3.RegressionFunc = new CurveFittingAlgorithms.ModelFunction[] { f0, f1, f2, f3, f4 };
            ds3.FittingMode = LineCharts.FittingCurve.FittingModeEnum.LinearRegression;
            ds3.LineThickness = 2;
            ds3.SeriesName = "Straight Fit-m5";
            ds3.Symbols.SymbolType = LineCharts.Symbols.SymbolTypeEnum.None;
            ds3.Symbols.FillColor = Brushes.Transparent;
            ds3.Symbols.BorderColor = Brushes.Transparent;
            for (int i = 0; i < x.Length; i++)
            {
                ds3.RawData.Add(x[i], y[i]);
            }
            myLineChart.SeriesCollection.AddRawData(ds3);

            LineCharts.FittingCurve ds4 = new LineCharts.FittingCurve();
            ds4.LineColor = Brushes.HotPink;
            ds4.XDrawStart = -1;
            ds4.XDrawEnd = 6;
            ds4.RateXValue = 3;
            ds4.LinePattern = LineCharts.LinePatternEnum.DashDot_X2;
            ds4.RegressionFunc = new CurveFittingAlgorithms.ModelFunction[] { f0, f1, f2, f3, f4, f5 };
            ds4.FittingMode = LineCharts.FittingCurve.FittingModeEnum.LinearRegression;
            ds4.LineThickness = 2;
            ds4.SeriesName = "Straight Fit-m6";
            ds4.Symbols.SymbolType = LineCharts.Symbols.SymbolTypeEnum.None;
            ds4.Symbols.FillColor = Brushes.Transparent;
            ds4.Symbols.BorderColor = Brushes.Transparent;
            for (int i = 0; i < x.Length; i++)
            {
                ds4.RawData.Add(x[i], y[i]);
            }
            myLineChart.SeriesCollection.AddRawData(ds4);
        }

        private static double f0(double x)
        {
            return 1.0;
        }
        private static double f1(double x)
        {
            return x;
        }
        private static double f2(double x)
        {
            return x * x;
        }
        private static double f3(double x)
        {
            return x * x * x;
        }
        private static double f4(double x)
        {
            return x * x * x * x;
        }
        private static double f5(double x)
        {
            return x * x * x * x * x;
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            myLineChart.SaveToFile(@"c:\abc.tif");
            //WordToPDF(@"c:\abc.docx");
        }
    }
}
